package com.lmoumou.lib_calendarview.utils

import android.util.Log
import android.util.SparseArray
import com.lmoumou.lib_calendarview.entity.DateBeen
import com.lmoumou.lib_calendarview.entity.DayAttrsBeen
import com.lmoumou.lib_calendarview.entity.DayBeen
import java.util.*
import kotlin.collections.LinkedHashMap

/**
 * @author:Lmoumou
 * @date:2019/11/5
 * 描述:
 **/
object CalendarUtils {

    private const val TAG = "CalendarUtils"
    /**
     * 获取当月所有的日期
     *
     * @param year 年
     * @param moth 月
     * */
    fun getDaysOfMoth(year: Int, moth: Int,map:LinkedHashMap<String,DayAttrsBeen>?=null): MutableList<DayBeen> {

        val calendar = Calendar.getInstance().apply {
            clear()
            set(Calendar.YEAR, year)
            set(Calendar.MONTH, moth - 1)
        }
        val dayNum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
        val dateBeen = DateBeen()
        dateBeen.year = year
        dateBeen.moth = moth
        dateBeen.isCurrentMoth = "$year-${if (moth<=9) "0$moth" else "$moth"}" == TimeUtils.long2Str(System.currentTimeMillis(), "yyyy-MM")
        val dayList = mutableListOf<DayBeen>()
        for (i in 1..dayNum) {
            val day = if (i <= 9) "0$i" else "$i"
            val date = "$year-${if (moth<=9) "0$moth" else "$moth"}-$day"
            val calendarThis = Calendar.getInstance().apply {
                clear()
                time = TimeUtils.strToDate(date)
            }
            val dayBeen = DayBeen()
            dayBeen.date = date
            dayBeen.day=day
            dayBeen.week = calendarThis.get(Calendar.DAY_OF_WEEK)
            dayBeen.dayAttrs=map?.get(date)
            dayBeen.isMothDay = true

            dayList.add(dayBeen)
        }

        //数据补充
        if (dayList.isNotEmpty()) {

            val endDayWeek = dayList[dayNum - 1].week

            val firstDayWeek = dayList[0].week
            val firstDate = TimeUtils.strToDate(dayList[0].date)


            for (i in 1 until firstDayWeek) {//开始不满一周的数据补上
                val calendarFirst = Calendar.getInstance().apply {
                    clear()
                    time = firstDate
                    add(Calendar.DAY_OF_MONTH, -i)
                }
                val dayBeen = DayBeen()
                dayBeen.isMothDay = false
                dayBeen.week = calendarFirst.get(Calendar.DAY_OF_WEEK)
                dayBeen.date = TimeUtils.long2Str(calendarFirst.time.time, "yyyy-MM-dd")
                dayList.add(0, dayBeen)
            }
            Log.e(TAG,"endDayWeek->$endDayWeek")
            if (endDayWeek<7){
                val endDate = TimeUtils.strToDate(dayList[dayNum - 1].date)
                for (i in (endDayWeek + 1)..7) {//最后不满一周的数据补上
                    val calendarFirst = Calendar.getInstance().apply {
                        clear()
                        time = endDate
                        add(Calendar.DAY_OF_MONTH, (i - endDayWeek))
                    }
                    val dayBeen = DayBeen()
                    dayBeen.isMothDay = false
                    dayBeen.week = calendarFirst.get(Calendar.DAY_OF_WEEK)
                    dayBeen.date = TimeUtils.long2Str(calendarFirst.time.time, "yyyy-MM-dd")
                    dayList.add(dayBeen)
                }
            }
        }



        dateBeen.dayList = dayList
        return dayList
    }
}

fun getWeekStr(week: Int): String = when (week) {
    1 -> "日"
    2 -> "一"
    3 -> "二"
    4 -> "三"
    5 -> "四"
    6 -> "五"
    7 -> "六"
    else -> "日"

}